package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.projectextensionrelation.ProjectExtensionRelationDTO;
import com.guldan.jaina.dto.projecttagrelation.ProjectTagRelationDTO;
import com.guldan.jaina.utils.ProjectExtensionRelationDomainConvertUtils;
import com.guldan.mapper.db.entity.ProjectExtensionDic;
import com.guldan.mapper.db.entity.ProjectExtensionRelation;
import com.guldan.mapper.db.entity.ProjectExtensionRelationExample;
import com.guldan.mapper.db.entity.ProjectTagRelation;
import com.guldan.mapper.db.entity.ProjectTagRelationExample;
import com.guldan.mapper.db.mapper.ProjectExtensionDicMapperExt;
import com.guldan.mapper.db.mapper.ProjectExtensionRelationMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class ProjectExtensionRelationManager extends BaseManager {

    @Autowired
    private ProjectExtensionRelationMapperExt projectExtensionRelationMapperExt;

    @Autowired
    private ProjectExtensionDicMapperExt projectExtensionDicMapperExt;

    //获取单个项目扩展属性关联信息
    public ProjectExtensionRelationDTO get(Integer projectExtensionRelationId) {
    	ProjectExtensionRelationDTO dto = ProjectExtensionRelationDomainConvertUtils.convert(projectExtensionRelationMapperExt.selectByPrimaryKey(projectExtensionRelationId));
    	ProjectExtensionDic projectExtensionDic = projectExtensionDicMapperExt.selectByPrimaryKey(dto.getExtId());
    	dto.setExtName(projectExtensionDic.getExtName());
    	return dto;
    }

    //新增/更新单个项目扩展属性关联，传入：ProjectExtensionRelationDTO
    public ProjectExtensionRelationDTO save(ProjectExtensionRelationDTO dto) {
    	ProjectExtensionRelation entity = null;

    	ProjectExtensionRelationExample example = new ProjectExtensionRelationExample();
    	ProjectExtensionRelationExample.Criteria criteria = example.createCriteria();
        if (dto.getProjectId() != null && dto.getProjectId() > 0) {
            criteria.andProjectIdEqualTo(dto.getProjectId());
        }
        if (dto.getExtId() != null && dto.getExtId() > 0) {
            criteria.andExtIdEqualTo(dto.getExtId());
        }
        List<ProjectExtensionRelation> list = projectExtensionRelationMapperExt.selectByExample(example);
        boolean needUpdate = false;
        if (list != null && list.size() > 0) {
        	for (ProjectExtensionRelation relation : list) {
        		if (!relation.getIsDelete()) {
        			entity = relation;
        			needUpdate = true;
        			break;
        		}
        	}
        }
        
        if (needUpdate) {
            entity.setExtId(dto.getExtId());
            entity.setProjectId(dto.getProjectId());
            entity.setIsDelete(false);
            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            projectExtensionRelationMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new ProjectExtensionRelation();
            entity.setExtId(dto.getExtId());
            entity.setProjectId(dto.getProjectId());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getCreator());
            entity.setCreateTime(DateUtil.getCurrentTime());
            projectExtensionRelationMapperExt.insertSelective(entity);
        }
        dto = ProjectExtensionRelationDomainConvertUtils.convert(entity);
        return dto;
    }

    //删除单个项目扩展属性关联信息
    public Boolean delete(ProjectExtensionRelation entity) {
        entity.setIsDelete(true);
        return projectExtensionRelationMapperExt.updateByPrimaryKeySelective(entity) > 0;
    }
    
    public List<ProjectExtensionRelation> getProjectExtensionRelationListByProjectId(Integer projectId) {
    	ProjectExtensionRelationExample example = new ProjectExtensionRelationExample();
    	ProjectExtensionRelationExample.Criteria criteria = example.createCriteria();
    	criteria.andProjectIdEqualTo(projectId);
    	criteria.andIsDeleteEqualTo(Boolean.FALSE);
    	List<ProjectExtensionRelation> projectExtensionRelationList = projectExtensionRelationMapperExt.selectByExample(example);

    	return projectExtensionRelationList;
    }


    /**
     * batch save
     * @param projectId
     * @param saveDtos
     * @return
     */
    public List<ProjectExtensionRelationDTO> saveList(Integer projectId, List<ProjectExtensionRelationDTO> saveDtos) {
        List<ProjectExtensionRelation> historyList = getProjectExtensionRelationListByProjectId(projectId);
        if (historyList != null && historyList.size() > 0) {
            for (ProjectExtensionRelation history : historyList) {
                Boolean needDelete = true;
                for (ProjectExtensionRelationDTO dto : saveDtos) {
                    if (dto.getExtId() != null && history.getExtId() != null && dto.getExtId().intValue() == history.getExtId().intValue()) {
                        needDelete = false;
                        break;
                    }
                }
                if (needDelete) {
                    delete(history);
                }
            }
        }
        for (ProjectExtensionRelationDTO dto : saveDtos) {
            dto.setProjectId(projectId);
            save(dto);
        }
        return saveDtos;
    }

}
